package class03_一些基础的数据结构.test;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Code07_TwoQueueImplementStack {
    public static class TwoQueueStack<T>{
        private Queue<T> queue;
        private Queue<T> help;
        public TwoQueueStack(){
            queue = new LinkedList<>();
            help = new LinkedList<>();
        }
        public void push(T val){
            queue.offer(val);
        }
        public T pop(){
            while (queue.size() > 1){
                help.offer(queue.poll());
            }
            T poll = queue.poll();
            Queue<T> tmp = queue;
            queue = help;
            help = tmp;
            return poll;
        }
        public T peek(){
            while (queue.size() > 1){
                help.offer(queue.poll());
            }
            T poll = queue.poll();
            if(poll != null){
                help.offer(poll);
            }
            Queue<T> tmp = queue;
            queue = help;
            help = tmp;
            return poll;
        }
        public boolean isEmpty(){
            return queue.isEmpty();
        }
    }

    public static void main(String[] args) {
        System.out.println("test begin");
        int testTimes = 1000000;
        int maxValue = 1000000;
        TwoQueueStack myStack = new TwoQueueStack();
        Stack stack = new Stack();
        for (int i = 0; i < testTimes; i++) {
            if(myStack.isEmpty()){
                int val = (int)(Math.random() * maxValue);
                myStack.push(val);
                stack.push(val);
            } else {
                if (Math.random() < 0.25) {
                    int num = (int) (Math.random() * maxValue);
                    myStack.push(num);
                    stack.push(num);
                } else if (Math.random() < 0.5) {
                    if (!myStack.peek().equals(stack.peek())) {
                        System.out.println("Oops");
                    }
                } else if (Math.random() < 0.75) {
                    if (!myStack.pop().equals(stack.pop())) {
                        System.out.println("Oops");
                    }
                } else {
                    if (myStack.isEmpty() != stack.isEmpty()) {
                        System.out.println("Oops");
                    }
                }
            }
        }
        System.out.println("test end");
    }
}
